﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DALScolarite;
namespace Scolarite
{
    public partial class InscrireEtudiants : Form
    {
        private const int MaxColumns = 64;
        protected string FileName;
        protected bool Modified;
        public modeload loadingmod;


        public InscrireEtudiants()
        {
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void btcsvfile_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog fd = new OpenFileDialog();
            fd.InitialDirectory = "c:\\";
            fd.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
            fd.FilterIndex = 1;
            fd.RestoreDirectory = true;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    CSVFileNameTextBox.Text = fd.FileName;
                    ReadFile(fd.FileName);
                     
                }
                catch (Exception err)
                {
                    //Inform the user if can't read the file
                    MessageBox.Show(err.Message);
                }
            }
        }

        private bool ReadFile(string filename)
        {
            Cursor = Cursors.WaitCursor;
            try
            {
                dataGridView1.Rows.Clear();
                List<string> columns = new List<string>();
                using (var reader = new CsvFileReader(filename))
                {
                    while (reader.ReadRow(columns))
                    {
                        scolarite_etudiants et = new scolarite_etudiants().verifetudiant(columns.ElementAt(0).ToString());
                       
                        int indexofrow = dataGridView1.Rows.Add(columns.ToArray());

                        if (et != null)
                        {
                            scolarite_inscription verifinscript = new scolarite_inscription().VerifInscripton(et.idetudiants,idAnneeUnivComboBox.SelectedValue.ToString());
                            if (verifinscript != null)
                            {
                                dataGridView1.Rows[indexofrow].DefaultCellStyle.BackColor = Color.Red;
                            }
                            else
                            {
                                dataGridView1.Rows[indexofrow].DefaultCellStyle.BackColor = Color.Yellow;
                            }
                        
                        
                        }
                      
                    }
                }
                FileName = filename;
                Modified = false;
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Error reading from {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            finally
            {
                Cursor = Cursors.Default;
            }
            return false;
        }

        

        private void InscrireEtudiants_Load(object sender, EventArgs e)
        {
            scolarite_classesBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_classes.ToList();
            scolarite_eltdiplomeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_eltdiplome.ToList();
            scolariteanneeunivBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_anneeuniv.ToList();
            if (this.loadingmod == modeload.AJOUT)
            {
                scolarite_inscriptionBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_inscription.ToList();
                scolarite_inscriptionBindingSource.AddNew();
            }
            /*else if (this.loadingmod == modeload.INFO && this.idEltDiplome != string.Empty)
            {
                scolarite_eltdiplomeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_eltdiplome.Where(et => et.idEltDiplome == idEltDiplome).ToList();

            }*/
        }

        private void Btinscirecsv_Click(object sender, EventArgs e)
        {

            uvtmoyEntities ent = new uvtmoyEntities();
         //   ent.Database.Connection.Open();
           // var tr = ent.Database.Connection.BeginTransaction();
            if (this.dataGridView1.RowCount > 0)
            {
                try
                {
                    foreach (DataGridViewRow r in this.dataGridView1.Rows)
                    {

                        if (r.Cells[0].Value != null)
                        {
                            scolarite_etudiants et = new scolarite_etudiants().verifetudiant(r.Cells[0].Value.ToString());
                            long idetudiant;
                            if (et == null)
                            {
                                scolarite_etudiants etudiant = new scolarite_etudiants
                                {
                                    cin = r.Cells[0].Value.ToString(),
                                    NomFR = r.Cells[1].Value.ToString(),
                                    PrenomFR = r.Cells[2].Value.ToString(),
                                    email = r.Cells[3].Value.ToString(),
                                    sex = r.Cells[4].Value.ToString(),
                                    idStatusEtudiant = r.Cells[5].Value.ToString()/* ,
                            dateinscription = DateTime.ParseExact(r.Cells[6].Value.ToString(), "dd/mm/yyyy", null) */

                                };
                                ent.scolarite_etudiants.Add(etudiant);
                                ent.SaveChanges();
                                idetudiant = etudiant.idetudiants;
                            }
                            else
                            {
                                idetudiant = et.idetudiants;
                            }
                           
                            scolarite_inscription verif = new scolarite_inscription().VerifInscripton(idetudiant, idAnneeUnivComboBox.SelectedValue.ToString());

                            if (verif==null)
                            {
                                scolarite_inscription inscript = new scolarite_inscription
                                {
                                    idAnneeUniv = idAnneeUnivComboBox.SelectedValue.ToString(),
                                    idClasse = idClasseComboBox.SelectedValue.ToString(),
                                    idEltDiplome = idEltDiplomeComboBox.SelectedValue.ToString(),
                                    idetudiants = idetudiant
                                };
                                ent.scolarite_inscription.Add(inscript);
                            }
                            ent.SaveChanges();
                        }
                    }
                    MessageBox.Show("Inscription a été bien effectué");
                  //  tr.Commit();
                }
                catch (DBConcurrencyException except)
                {
                  //  tr.Rollback();
                    
                  //  ent.Database.Connection.Close();
                    MessageBox.Show(except.Message);
                }

            }
        }

        

        private void btaddnewClasse_Click(object sender, EventArgs e)
        {
            FicheClasse fiche = new FicheClasse();
            fiche.MdiParent = this.MdiParent;
            fiche.loadingmod = modeload.AJOUT;
            fiche.Show();

        }

        private void btaddnewdiplome_Click(object sender, EventArgs e)
        {
            FicheFormation fiche = new FicheFormation();
            fiche.MdiParent = this.MdiParent;
            fiche.loadingmod = modeload.AJOUT;
            fiche.Show();
        }

        private void idEltDiplomeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            scolarite_classes classetud = new scolarite_classes();

        }

        private void idAnneeUnivComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
